package com.olap.starter.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.olap.starter.common.service.crud.CRUDService;
import com.olap.starter.common.util.ExcelUtil;
import com.olap.starter.common.vo.*;
import com.olap.starter.core.response.Result;
import com.olap.starter.core.response.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.olap.starter.common.util.AESCRUDUtils.decryptWithDecompress;

/**
 * @author WBG
 * @date 2024/2/2 11:21
 * @describe
 */
@RestController
@RequestMapping("/online/crudAes")
public class CRUDAESController {

    @Autowired
    private CRUDService crudService;

    @PostMapping("/query")
    public Result query(@RequestBody String params) {
        return ResultUtil.success(crudService.queryData(decryptWithDecompressObject(params, QueryParameter.class)));
    }

    public static <T> T decryptWithDecompressObject(String params, Class<T> clazz) {
        params = decryptWithDecompress(params);
        return JSONObject.parseObject(params, clazz);
    }

    @PostMapping("/save")
    public Result save(@RequestBody String params) {
        return crudService.save(decryptWithDecompressObject(params, SaveParameter.class));
    }

    @PostMapping("/saveMainAndSubs")
    public Result saveMainAndSubs(@RequestBody String params) {
        return crudService.saveMainAndSubs(decryptWithDecompressObject(params, SaveMainAndSubTable.class));
    }
    @PostMapping("/deleteMainAndSubs")
    public Result deleteMainAndSubs(@RequestBody String params) {
        return crudService.deleteMainAndSubs(decryptWithDecompressObject(params, DeleteMainAndSubTable.class));
    }


    @PostMapping("/batchSave")
    public Result batchSave(@RequestBody String params) {
        return crudService.batchSave(decryptWithDecompressObject(params, BatchSaveParameter.class));
    }

    @PostMapping("/modify")
    public Result modify(@RequestBody String params) {
        return crudService.modify(decryptWithDecompressObject(params, ModifyParameter.class));
    }

    @PostMapping("/batchModify")
    public Result batchModify(@RequestBody String params) {
        return crudService.batchModify(decryptWithDecompressObject(params, BatchUpdateParameter.class));
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody String params) {
        return crudService.delete(decryptWithDecompressObject(params, DeleteParameter.class));
    }

    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody String params) {
        return crudService.batchDelete(decryptWithDecompressObject(params, BatchDeleteParameter.class));
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file) {
        try {
            return ResultUtil.success(ExcelUtil.WorkbookToListMap(ExcelUtil.importExcel(file)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("导入失败！" + e.getMessage());
        }
    }
}
